package com.itheima.reggie.service.impl;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;



    @Override
    public R<Employee> login(Employee employee) {
        //todo 1.校验参数  null  or  ""
        if(StringUtils.isBlank(employee.getUsername()) || StringUtils.isBlank(employee.getPassword())){
            return R.error("参数异常");
        }
//        //todo 2.密码进行MD5加密
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
//
//        //todo 3.根据用户名查询是否存在此用户
        Employee emp = employeeMapper.selectEmployeeByUserName(employee.getUsername());

        if(emp == null){
            return R.error("员工不存在");
        }
        //todo 4. 如果用户存在，校验密码是否一致
        if(!password.equals(emp.getPassword())){
            return R.error("密码错误");
        }
        //todo 5.判断用户是否被禁用
        if(emp.getStatus() == 0){
            return R.error("账号已禁用");
        }
        //todo 6.返回登录的员工信息
        return R.success(emp);
    }
}
